typescript-memoize
A memoize decorator for Typescript.
Installation
npm install --save typescript-memoize
Usage:
@Memoize(hashFunction?: (...args: any[]) => any)
You can use it in four ways:
- Memoize a
get
accessor, - Memoize a method which takes no parameters,
- Memoize a method which varies based on its first parameter only,
- Memoize a method which varies based on some combination of parameters
You can call memoized methods within the same class, too. This could be useful if you want to memoize the return value for an entire data set, and also a filtered or mapped version of that same set.
Memoize a get
accessor, or a method which takes no parameters
These both work the same way. Subsequent calls to a memoized method without parameters, or to a get
accessor, always return the same value.
I generally consider it an anti-pattern for a call to a get
accessor to trigger an expensive operation. Simply adding Memoize()
to a get
allows for seamless lazy-loading.
import {Memoize,MemoizeExpiring} from 'typescript-memoize';
class SimpleFoo {
@Memoize()
public getAllTheData() {
return data;
}
@MemoizeExpiring(5000)
public getDataForSomeTime() {
return data;
}
@Memoize()
public get someValue() {
return value;
}
}
And then we call them from somewhere else in our code:
let simpleFoo = new SimpleFoo();
let methodVal1 = simpleFoo.getAllTheData();
let methodVal2 = simpleFoo.getAllTheData();
let getterVal1 = simpleFoo.someValue;
let getterVal2 = simpleFoo.someValue;
Memoize a method which varies based on its first parameter only
Subsequent calls to this style of memoized method will always return the same value.
I'm not really sure why anyone would use this approach to memoize a method with more than one parameter, but it's possible.
import {Memoize} from 'typescript-memoize';
class ComplicatedFoo {
@Memoize()
public getAllTheData() {
return data;
}
@Memoize()
public getSomeOfTheData(id: number) {
let allTheData = this.getAllTheData();
return data;
}
@Memoize()
public getGreeting(name: string, planet: string) {
return 'Hello, ' + name + '! Welcome to ' + planet;
}
}
We call these methods from somewhere else in our code:
let complicatedFoo = new ComplicatedFoo();
let oneParam1 = complicatedFoo.getSomeOfTheData();
let oneParam2 = complicatedFoo.getSomeOfTheData();
let greeterVal1 = complicatedFoo.getGreeting('Darryl', 'Earth');
let greeterVal2 = complicatedFoo.getGreeting('Darryl', 'Mars');
Memoize a method which varies based on some combination of parameters
Pass in a hashFunction
which takes the same parameters as your target method, to memoize values based on all parameters, or some other custom logic
import {Memoize} from 'typescript-memoize';
class MoreComplicatedFoo {
@Memoize((name: string, planet: string) => {
return name + ';' + planet;
})
public getBetterGreeting(name: string, planet: string) {
return 'Hello, ' + name + '! Welcome to ' + planet;
}
@Memoize(() => {
return new Date();
})
public memoryLeak(greeting: string) {
return greeting + '!!!!!';
}
@Memoize({
expiring: 10000,
hashFunction: (name: string, planet: string) => {
return name + ';' + planet;
}
})
public getSameBetterGreeting(name: string, planet: string) {
return 'Hello, ' + name + '! Welcome to ' + planet;
}
}
We call these methods from somewhere else in our code. By now you should be getting the idea:
let moreComplicatedFoo = new MoreComplicatedFoo();
let greeterVal1 = moreComplicatedFoo.getBetterGreeting('Darryl', 'Earth');
let greeterVal2 = moreComplicatedFoo.getBetterGreeting('Darryl', 'Mars');
let greeting = moreComplicatedFoo.memoryLeak('Hello');
Memoize accepts one or more "tag" strings that allow the cache to be invalidated on command
Passing an array with one or more "tag" strings these will allow you to later clear the cache of results associated with methods or the get
accessors using the clear()
function.
The clear()
function also requires an array of "tag" strings.
import {Memoize} from 'typescript-memoize';
class ClearableFoo {
@Memoize({ tags: ["foo", "bar"] })
public getClearableGreeting(name: string, planet: string) {
return 'Hello, ' + name + '! Welcome to ' + planet;
}
@Memoize({ tags: ["bar"] })
public getClearableSum(a: number, b: number) {
return a + b;
}
}
We call these methods from somewhere else in our code.
import {clear} from 'typescript-memoize';
let clearableFoo = new ClearableFoo();
let greeterVal1 = clearableFoo.getClearableGreeting('Darryl', 'Earth');
let greeterVal2 = clearableFoo.getClearableGreeting('Darryl', 'Mars');
let sum1 = clearableFoo.getClearableSum(2, 1);
let sum2 = clearableFoo.getClearableSum(2, 2);
clear(["foo"]);
let greeterVal3 = clearableFoo.getClearableGreeting('Darryl', 'Mars');
let sum3 = clearableFoo.getClearableSum(2, 2);
clear(["bar"]);
let greeterVal4 = clearableFoo.getClearableGreeting('Darryl', 'Earth');
let sum4 = clearableFoo.getClearableSum(2, 2);